package com.bibrApp.difu.actvity.fund_security_code_mvp;


import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.bibrApp.difu.Config;
import com.bibrApp.difu.DfApp;
import com.bibrApp.difu.R;
import com.bibrApp.difu.Response.LoginResponseBody;
import com.bibrApp.difu.actvity.login_mvp.LoginActivity;
import com.bibrApp.difu.base.BaseActivity;
import com.bibrApp.difu.dialog.MyCustomDialog;
import com.bibrApp.difu.utils.BiBrShare;
import com.bibrApp.difu.utils.ShareUtils;
import com.bibrApp.difu.utils.StrValidateUtils;
import com.bibrApp.difu.utils.ToastUtils;
import com.bibrApp.difu.utils.VerifyCodeUtils;
import com.gyf.barlibrary.ImmersionBar;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.OnClick;

public class FundSecurityCodeActivity extends BaseActivity<FundSecurityCodePresenter> implements IFundSecurityCodeView, TextWatcher {

    private ImmersionBar mImmersionBar;
    private Context mContext;
    @BindView(R.id.fund_security_title)
    TextView fund_security_title;
    @BindView(R.id.commit_btn)
    Button commitBtn;
    @BindView(R.id.setting_pwd_et)
    EditText firstET;
    @BindView(R.id.setting_pwd_again_et)
    EditText againET;
    @BindView(R.id.setting_pwd_hint_tv)
    TextView firstHintTV;
    @BindView(R.id.setting_pwd_hint_again_tv)
    TextView againHintTV;
    @BindView(R.id.setting_verify_code_et)
    EditText verifyCodeET;

    @BindView(R.id.setting_verify_code_get_code_tv)
    TextView getCodeTV;
    private CountTimer timer;
    private DfApp dfApp;
    private ShareUtils biBrShare;

    private class CountTimer extends CountDownTimer {

        public CountTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onFinish() {
            // TODO Auto-generated method stub
            getCodeTV.setEnabled(true);
            getCodeTV.setClickable(true);
            DfApp dfApp = (DfApp) getApplication();
            LoginResponseBody loginResponseBody = dfApp.getmData();
            if (loginResponseBody != null) {
                if(loginResponseBody.getCenterUserMain().getRegType() == 0){
                    getCodeTV.setText("获取短信验证码");
                }else {
                    getCodeTV.setText("获取邮箱验证码");
                }
            }
            getCodeTV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    presenter.getCode(VerifyCodeUtils.getAccount((DfApp) getApplication()));
                }
            });
        }

        @Override
        public void onTick(long millisUntilFinished) {
            // TODO Auto-generated method stub
            getCodeTV.setText((millisUntilFinished / 1000) + "S" + "重新获取");
        }
    }

    @Override
    protected void initPresenter() {
        presenter = new FundSecurityCodePresenter(this, this);
        presenter.init();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_fund_security_code;
    }

    @Override
    public void initView() {
        mContext = this;
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.statusBarColor(R.color.white)
                .statusBarDarkFont(true)
                .fitsSystemWindows(true)
                .init();
        biBrShare = BiBrShare.getShare(mContext, Config.FILE_ACCOUNT);
        dfApp = (DfApp) getApplication();
        LoginResponseBody loginResponseBody = dfApp.getmData();
        if (loginResponseBody != null) {
            if (!TextUtils.isEmpty(loginResponseBody.getCenterUserMain().getTradePassword())) {
                fund_security_title.setText("修改资金安全码");
            } else {
                fund_security_title.setText("设置资金安全码");
            }
            if(loginResponseBody.getCenterUserMain().getRegType() == 0){
                getCodeTV.setText("获取短信验证码");
            }else {
                getCodeTV.setText("获取邮箱验证码");
            }
        }

        firstET.addTextChangedListener(this);
        againET.addTextChangedListener(this);
        verifyCodeET.addTextChangedListener(this);

    }


    @Override
    public void error(String msg) {
        ToastUtils.ShowToast(mContext, msg);
    }

    @Override
    public void success(String msg) {
        presenter.getUserInfo(dfApp.getUserId());
    }

    //获取验证码成功
    @Override
    public void successOfCode(String msg) {
        if (timer == null) {
            timer = new CountTimer(60000, 1000);
        }
        timer.start();
        getCodeTV.setEnabled(false);
        getCodeTV.setClickable(false);
    }

    @Override
    public void successOfGetUserInfo(LoginResponseBody.CenterUserMain userInfo) {
        LoginResponseBody data = dfApp.getmData();
        if (userInfo != null) {
            biBrShare.deleteData(Config.FILE_ACCOUNT_KEY);
            data.setCenterUserMain(userInfo);
            biBrShare.saveData(data, Config.FILE_ACCOUNT_KEY);
        }
        finish();
    }

    @OnClick(R.id.return_iv)
    public void onClickBack() {
        finish();
    }
    @OnClick(R.id.setting_verify_code_get_code_tv)
    public void onClickGetCode(){
        if(verify()){
            presenter.getCode(VerifyCodeUtils.getAccount((DfApp) getApplication()));
        }
    }
    @OnClick(R.id.commit_btn)
    public void onClickCommitBtn() {
        if(!verify()){
            return;
        }
        if (!TextUtils.isEmpty(verifyCodeET.getText().toString()) && verifyCodeET.getText().toString().length() >= 4) {
            presenter.setFSCode(pwdStr, verifyCodeET.getText().toString());
        } else {
            ToastUtils.ShowToast(mContext, "请输入正确的验证码");
        }
    }

    /**
     * 验证是否通过,设置或修改资金安全码
     */
    private String pwdStr;

    private boolean verify() {
        String firstETStr = firstET.getText().toString().replaceAll(" ", "");
        String againETStr = againET.getText().toString().replaceAll(" ", "");
        if (!TextUtils.isEmpty(firstETStr)) {
            if (firstETStr.length() > 5 && firstETStr.length() < 19 && StrValidateUtils.isLetterDigit(firstETStr)) {
                if (firstETStr.equals(againETStr)) {
                    pwdStr = againETStr;
                    return true;
                } else {
                    againHintTV.setVisibility(View.VISIBLE);
                    againHintTV.setText("请输入与第一次相同的资金安全码");
                }
            } else {
                firstHintTV.setVisibility(View.VISIBLE);
                firstHintTV.setText("请输入6-18位数字字符组合且必须包含数字和字母");
            }

        } else {
            firstHintTV.setVisibility(View.VISIBLE);
            firstHintTV.setText("资金安全码不能为空");
        }
        return false;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mImmersionBar != null) {
            mImmersionBar.destroy();
        }
        if(timer != null){
            timer.cancel();
        }
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int start, int before, int count) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int start, int count, int after) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        String firstETStr = firstET.getText().toString().replaceAll(" ", "");
        String againETStr = againET.getText().toString().replaceAll(" ", "");
        String codeStr = verifyCodeET.getText().toString().replaceAll(" ", "");
        if (!TextUtils.isEmpty(firstETStr) && !TextUtils.isEmpty(againETStr) && !TextUtils.isEmpty(codeStr)) {
            commitBtn.setEnabled(true);
            commitBtn.setClickable(true);
        } else {
            commitBtn.setEnabled(false);
            commitBtn.setClickable(false);
        }
    }

}
